package fr.emse.tatiana.replayable.analysis.graph;

import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.AbstractEditPolicy;

public class AppEditPolicy extends AbstractEditPolicy {

	@Override
	public Command getCommand(Request r) {
		if (r.getType().equals("edit")) {
			return createEditCommand(r);
		} else if (r.getType().equals("delete")) {
			return createDeleteCommand(r);
		}
		return null;
	}

	protected Command createDeleteCommand(Request r) {
		if (getHost() instanceof ConnectionEditPart) {
			ConnectionEditPart part = (ConnectionEditPart) getHost();
			ConnectionDeleteCommand cmd = new ConnectionDeleteCommand(part.getCastedModel());
			return cmd;
		}
		return null;
	}

	protected Command createEditCommand(Request r) {
		if (getHost() instanceof EventGraphPart) {
			EventGraphPart part = (EventGraphPart) getHost();
			EventChangeTextCommand cmd = new EventChangeTextCommand();
			cmd.setModel(part.getAnalysis(),part.getAnchor());
			cmd.setText((String) r.getExtendedData().get("editText"));
			return cmd;
		} else if (getHost() instanceof ConnectionEditPart) {
			ConnectionEditPart part = (ConnectionEditPart) getHost();
			ConnectionChangeTextCommand cmd = new ConnectionChangeTextCommand();
			cmd.setModel(part.getCastedModel());
			cmd.setText((String) r.getExtendedData().get("editText"));
			return cmd;
		}
		return null;
	}

}
